package com.dev;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Space;
import android.widget.Toast;
import com.fb.ConfirmDeleteFile;
import com.fb.FileNavigator;
import com.fb.QueryActionForFile;
import com.fb.QueryCreate;
import com.fb.QueryRenameFile;
import com.fb.WhichTypeWillBeCreate;
import com.log;
import com.servlet.GUIHttpHost;
import java.io.File;
import java.util.ArrayList;
import android.content.pm.ShortcutManager;
public class MainActivity extends Activity { 
	public final static String CLIPBOARD_TAG="com.remotefile.clipBoardTag";
	public final static int CYAN=0xff00bbff;
	public final static int FILE_LENGTH_LIM=500*1024;
	public final static int PURPLE=0xffff00bb;
	public final static int titlebarBackgroundColor=0xff298447;
	public SharedPreferences shr;
	public ClipboardManager cm;
	public float dp;
	public int titlebarHeight;
	public int dialogTitleBarHeight;
    public int dialogRowHeight;
	public int wp;
	public int hp;
	public TabData.TabList tabList;
	public TabData tdm;
	public ArrayList<FileClipData> fileClipBoard=new ArrayList<FileClipData>();
	public File workDir;
	public InputMethodManager imm;
	public WindowManager wm;
	public PackageManager pm;
	public GUI gui;
	public GUISettings settings;
	public GUICollections collections;
	public GUIHttpHost httpHost;
    public ShowFileDetails fileDetails;
    public ShowFileClipBoard showFileClipBoard;
    public ShortcutManager shortcutManager;
    public ConfirmDeleteFile confirmDeleteFile;
    public ConfirmFinishEditing confirmFinishEditing;
    public QueryCreate queryNewFile;
    public QueryOpenFile queryOpenFile;
    public QueryActionForFile queryActionForFile;
    public QueryPaste queryPaste;
    public QueryRenameFile qrf;
    public QueryManageShortcut qms;
    public FileNavigator.QueryForTab qft;
    public FileNavigator.QueryRename qrt;
    public WhichTypeWillBeCreate whichTypeWillBeCreate;
	public Search search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		log.c=this;
        log.see();
		wm=getWindowManager();
		cm=getSystemService(ClipboardManager.class);
		DisplayMetrics dm=new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);
		dp=dm.density;
		dialogTitleBarHeight=(int)(30*dp);
        dialogRowHeight=(int)(28*dp);
		wp=dm.widthPixels;
		hp=dm.heightPixels;
		titlebarHeight=(int)(33*dp);
		shr=getSharedPreferences("global",MODE_PRIVATE);
		pm=getPackageManager();
		imm=(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
		gui=new GUI(this);
		setContentView(gui);
        workDir=new File(Environment.getExternalStorageDirectory(),getPackageName());
		if(Environment.isExternalStorageManager()){
            tdm=new TabData(this);
			
			if(!workDir.exists()){
				try{
					workDir.mkdir();
				}catch(Exception e){
					log.alert(e.toString());
				}
			}
			tabList=tdm.loadTabList();
            //AlertTabLoading atl=new AlertTabLoading(this);
            //atl.show();
			//int currentTab=shr.getInt("current_tab",-1);
			for(int index=0,len=tabList.ts.size();true;index++){
				if(index<len){
					Object ob=tabList.ts.get(index);
					if(ob instanceof TabData.FileBrowserInfo){
						TabData.FileBrowserInfo fbi=(TabData.FileBrowserInfo)ob;
						FileBrowserTab fbt=new FileBrowserTab(this);
						fbt.fbi=fbi;
						fbt.setText(fbi.home.getName());
						fbt.setOnClickListener(gui.TabOnClick);
						gui.tabs.addView(fbt,TabBar.tablp);
						/*if(index==currentTab){
							gui.TabOnClick.onClick(fbt);
						}*/
					}else if(ob instanceof TabData.MediaAudioBrowser){
						FileBrowserTab fbt=new FileBrowserTab(this);
						fbt.setOnClickListener(gui.TabOnClick);
						fbt.setText(TabData.MediaAudioBrowser.tabTitle);
						fbt.der=10;
						fbt.textUnderline=true;
						gui.tabs.addView(fbt,TabBar.tablp);
						/*if(index==currentTab){
							gui.TabOnClick.onClick(fbt);
						}*/
					}else if(ob instanceof TabData.MediaImageInfo){
						FileBrowserTab fbt=new FileBrowserTab(this);
						fbt.setOnClickListener(gui.TabOnClick);
						fbt.setText(TabData.MediaAudioBrowser.tabTitle);
						fbt.der=20;
						fbt.textUnderline=true;
						gui.tabs.addView(fbt,TabBar.tablp);
						/*if(index==currentTab){
							gui.TabOnClick.onClick(fbt);
						}*/
					}else if(ob instanceof TabData.JavaEditorInfo){
						JavaEditorTab jet=new JavaEditorTab(this);
						TabData.JavaEditorInfo jei=(TabData.JavaEditorInfo)ob;
						jet.jei=jei;
						jet.setText(jei.src.getName());
						jet.setOnClickListener(gui.TabOnClick);
						gui.tabs.addView(jet,TabBar.tablp);
					}else if(ob instanceof TabData.AppListInfo){
						//TabData.AppListInfo ali=(TabData.AppListInfo)ob;
						AppListTab alt=new AppListTab(this);
						alt.setOnClickListener(gui.TabOnClick);
						gui.tabs.addView(alt,TabBar.tablp);
					}else if(ob instanceof TabData.ImageManipulation){
						ImageManipulationTab imt=new ImageManipulationTab(this);
						TabData.ImageManipulation im=(TabData.ImageManipulation)ob;
						if(im.src==null){
							log.alert("can not create ImageManipulationTab and correspond page,src file is null");
						}else{
							imt.im=im;
							imt.setText(im.src.getName());
							imt.setOnClickListener(gui.TabOnClick);
							gui.tabs.addView(imt,TabBar.tablp);			
						}
					}else if(ob instanceof TabData.CEditor){
						CEditorTab cet=new CEditorTab(this);
						TabData.CEditor ce=(TabData.CEditor)ob;
						if(ce.src==null){
							log.alert("can not load c editor page,the path of source on null pointer");
						}else{
							cet.ce=ce;
							cet.setText(ce.src.getName());
							cet.setOnClickListener(gui.TabOnClick);
							gui.tabs.addView(cet,TabBar.tablp);
						}
					}
				}else{
					break;
				}
			}
			//log.time("load data and create corresponding views");
		}else{
			Intent intent=new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,Uri.parse("package:"+getPackageName()));
			startActivityForResult(intent,0);
		}
    }
	@Override
	protected void onStart() {
		super.onStart();
		//Looper.getMainLooper().quit();
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig){
		super.onConfigurationChanged(newConfig);
		if(newConfig.keyboard==Configuration.KEYBOARD_QWERTY){
			Toast.makeText(this,"外部键盘已连接",0).show();
		}
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode){
            case 0:
                if(resultCode==RESULT_OK){
                    tdm=new TabData(this);
                }
                break;
            case 10:
                Toast.makeText(this,"创建快捷方式成功",0).show();
                break;
        }
    }
    
	@Override
	protected void onDestroy() {
		super.onDestroy();
		setContentView(new Space(this));
	}
	@Override
	public void onPointerCaptureChanged(boolean hasCapture) {
	}
    
} 
